﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using CRMFramework;

namespace CRMBiz.Configuration
{
    public static class WebServiceConfiguration
    {
        public static CRMEntity.Configuration.WebServiceConfiguration WebServiceSetParamter(string servicename, List<List<string>> DynamicParamter, string Company)
        {
            CRMEntity.Configuration.WebServiceConfiguration webserviceConfiguration;
            try
            {
                new CRMEntity.Configuration.WebServiceConfiguration();
                webserviceConfiguration = GetSingleWebService(servicename);
                string OutPutXML = webserviceConfiguration.ServiceInputXML;
                foreach (DataRow dr in webserviceConfiguration.ParameterList.Rows)
                {
                    if (dr["IsDynamic"].ToString() == "0")
                    {
                        if (dr["ParameterName"].ToString() == "subscriber-identifier" || dr["ParameterName"].ToString() == "security")
                        {
                            if (dr["Company"].ToString() == Company)
                            {
                                string source = dr["ParameterReplaceString"].ToString();
                                string target = dr["ParameterValue"].ToString();
                                OutPutXML = OutPutXML.Replace(dr["ParameterReplaceString"].ToString(), dr["ParameterValue"].ToString());
                            }
                        }
                        else
                        {
                            string source = dr["ParameterReplaceString"].ToString();
                            string target = dr["ParameterValue"].ToString();
                            OutPutXML = OutPutXML.Replace(dr["ParameterReplaceString"].ToString(), dr["ParameterValue"].ToString());
                        }
                    }
                    else
                    {
                        foreach (List<string> ParameterRowList in DynamicParamter)
                        {
                            if (ParameterRowList[0] == dr["ParameterReplaceString"].ToString())
                            {
                                OutPutXML = OutPutXML.Replace(dr["ParameterReplaceString"].ToString(), ParameterRowList[1]);
                                break;
                            }
                        }
                    }
                }
                webserviceConfiguration.ServiceInputXML = OutPutXML;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return webserviceConfiguration;
        }

        public static CRMEntity.Configuration.WebServiceConfiguration GetSingleWebService(string ServiceName)
        {
            CRMEntity.Configuration.WebServiceConfiguration webserviceConfiguration = new CRMEntity.Configuration.WebServiceConfiguration(); ;
            try
            {
                string webserviceGetSQL = @"select ServiceID,serviceName,ServiceURL,
                                        ServiceInputXML, ServiceOutputXML from webserviceconfiguration where ServiceName = @ServiceName";
                string webServiceGetParameterSQL = @"select ParameterID,ParameterName,ParameterReplaceString,ParameterValue,ParameterType,IsDynamic,ServiceID,Company
                                                from webserviceconfigurationparameter where ServiceID = @ServiceID";
                List<List<string>> lstParent = new List<List<string>>();
                List<string> lstChild = new List<string>();
                lstChild.Add("ServiceName");
                lstChild.Add(ServiceName);
                lstParent.Add(lstChild);

                DataTable dt_parent = DBhelper.ExecuteDataTable(webserviceGetSQL, lstParent);
                List<List<string>> lstParentID = new List<List<string>>();
                List<string> lstChildID = new List<string>();
                lstChildID.Add("ServiceID");
                lstChildID.Add(dt_parent.Rows[0]["ServiceID"].ToString());
                lstParentID.Add(lstChildID);

                DataTable dt_child = DBhelper.ExecuteDataTable(webServiceGetParameterSQL, lstParentID);
                webserviceConfiguration.ServiceID = Convert.ToInt32(dt_parent.Rows[0]["ServiceID"].ToString());
                webserviceConfiguration.ServiceName = dt_parent.Rows[0]["ServiceName"].ToString();
                webserviceConfiguration.ServiceURL = dt_parent.Rows[0]["ServiceURL"].ToString();
                webserviceConfiguration.ServiceInputXML = dt_parent.Rows[0]["ServiceInputXML"].ToString();
                webserviceConfiguration.ParameterList = dt_child;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return webserviceConfiguration;//populate this object on next day. 
        }

        public static DataTable GetAllWebservice()
        {
            string webserviceGetSQL = @"select ServiceID,serviceName,ServiceURL,
                                        ServiceInputXML, ServiceOutputXML from webserviceconfiguration";
            DataTable dt = CRMFramework.DBhelper.ExecuteDataTable(webserviceGetSQL);
            return dt;
        }

        public static CRMEntity.Configuration.WebServiceConfiguration GetSingleWebService(Int32 ServiceID)
        {
            string webserviceGetSQL = @"select ServiceID,serviceName,ServiceURL,
                                        ServiceInputXML, ServiceOutputXML from webserviceconfiguration where ServiceID = @ServiceID";
            string webServiceGetParameterSQL = @"select ParameterID,ParameterName,ParameterReplaceString,ParameterValue,ParameterType,IsDynamic,ServiceID
                                                from webserviceconfigurationparameter where ServiceID = @ServiceID";
            List<List<string>> lstParent = new List<List<string>>();
            List<string> lstChild = new List<string>();
            lstChild.Add("ServiceID");
            lstChild.Add(ServiceID.ToString());
            lstParent.Add(lstChild);

            DataTable dt_parent = CRMFramework.DBhelper.ExecuteDataTable(webserviceGetSQL, lstParent);
            DataTable dt_child = CRMFramework.DBhelper.ExecuteDataTable(webServiceGetParameterSQL, lstParent);

            CRMEntity.Configuration.WebServiceConfiguration webserviceConfiguration = new CRMEntity.Configuration.WebServiceConfiguration();
            webserviceConfiguration.ServiceID = Convert.ToInt32(dt_parent.Rows[0]["ServiceID"].ToString());
            webserviceConfiguration.ServiceName = dt_parent.Rows[0]["ServiceName"].ToString();
            webserviceConfiguration.ServiceURL = dt_parent.Rows[0]["ServiceURL"].ToString();
            webserviceConfiguration.ServiceInputXML = dt_parent.Rows[0]["ServiceInputXML"].ToString();
            webserviceConfiguration.ParameterList = dt_child;


            return webserviceConfiguration;//populate this object on next day. 

        }

        public static void InsertServiceConfiguration(CRMEntity.Configuration.WebServiceConfiguration webservice)
        {
            string webserviceSaveSQL = "insert into webserviceconfiguration (ServiceName,ServiceURL,ServiceAccessUser,ServiceAccessPass,ServiceInputXML)"
                                        + "values (@ServiceName,@ServiceURL,@ServiceAccessUser,@ServiceAccessPass,@ServiceInputXML)";

            List<List<string>> lstParent = new List<List<string>>();
            List<string> lstChild = new List<string>();
            lstChild.Add("ServiceName");
            lstChild.Add(webservice.ServiceName);
            lstParent.Add(lstChild);
            lstChild = new List<string>();
            lstChild.Add("ServiceURL");
            lstChild.Add(webservice.ServiceURL);
            lstParent.Add(lstChild);
            lstChild = new List<string>();
            lstChild.Add("ServiceInputXML");
            lstChild.Add(webservice.ServiceInputXML);
            lstParent.Add(lstChild);

            CRMFramework.DBhelper.ExecuteNonQuery(webserviceSaveSQL, lstParent);

        }

        public static void UpdateWebService(CRMEntity.Configuration.WebServiceConfiguration webservice)
        {
            string webserviceSaveSQL = @"update webserviceconfiguration set ServiceName=@ServiceName,ServiceURL=@ServiceURL,
                                        ServiceInputXML=@ServiceInputXML where ServiceID=@ServiceID";

            List<List<string>> lstParent = new List<List<string>>();
            List<string> lstChild = new List<string>();
            lstChild.Add("ServiceName");
            lstChild.Add(webservice.ServiceName);
            lstParent.Add(lstChild);
            lstChild = new List<string>();
            lstChild.Add("ServiceURL");
            lstChild.Add(webservice.ServiceURL);
            lstParent.Add(lstChild);
            lstChild = new List<string>();
            lstChild.Add("ServiceInputXML");
            lstChild.Add(webservice.ServiceInputXML);
            lstParent.Add(lstChild);
            lstChild = new List<string>();
            lstChild.Add("ServiceID");
            lstChild.Add(webservice.ServiceID.ToString());
            lstParent.Add(lstChild);

            CRMFramework.DBhelper.ExecuteNonQuery(webserviceSaveSQL, lstParent);

        }

        public static void InsertServiceParameter(CRMEntity.Configuration.WebServiceParmater ServiceParameter)
        {
            string webserviceParamterSaveSQL = "insert into webserviceconfigurationparameter (ParameterName,ParameterReplaceString,ParameterValue,ServiceId,IsDynamic)"
                                        + "values (@ParameterName,@ParameterReplaceString,@ParameterValue,@ServiceId,@IsDynamic)";

            List<List<string>> lstParent = new List<List<string>>();
            List<string> lstChild = new List<string>();
            lstChild.Add("ParameterName");
            lstChild.Add(ServiceParameter.ParameterName);
            lstParent.Add(lstChild);
            lstChild = new List<string>();
            lstChild.Add("ParameterReplaceString");
            lstChild.Add(ServiceParameter.ParameterReplaceString);
            lstParent.Add(lstChild);
            lstChild = new List<string>();
            lstChild.Add("ParameterValue");
            lstChild.Add(ServiceParameter.ParameterValue);
            lstParent.Add(lstChild);
            lstChild = new List<string>();
            lstChild.Add("ServiceId");
            lstChild.Add(ServiceParameter.ServiceID.ToString());
            lstParent.Add(lstChild);
            lstChild = new List<string>();
            lstChild.Add("IsDynamic");
            lstChild.Add(ServiceParameter.IsDynamic.ToString());
            lstParent.Add(lstChild);

            CRMFramework.DBhelper.ExecuteNonQuery(webserviceParamterSaveSQL, lstParent);
        }

        public static void DeleteServiceParamter(Int32 ParamterID)
        {
            string webserviceParamterDeleteSQL = @"delete from webserviceconfigurationparameter where ParameterID=@ParameterID";
            List<List<string>> lstParent = new List<List<string>>();
            List<string> lstChild = new List<string>();
            lstChild.Add("ParameterID");
            lstChild.Add(ParamterID.ToString());
            lstParent.Add(lstChild);

            CRMFramework.DBhelper.ExecuteNonQuery(webserviceParamterDeleteSQL, lstParent);

        }

        public static void UpdateServiceParameter(CRMEntity.Configuration.WebServiceParmater ServiceParameter)
        {
            string webserviceParamterSaveSQL = @"update webserviceconfigurationparameter set ParameterName=@ParameterName,
                                                ParameterReplaceString=@ParameterReplaceString,ParameterValue=@ParameterValue,
                                                IsDynamic=@IsDynamic where ParameterID=@ParameterID";

            List<List<string>> lstParent = new List<List<string>>();
            List<string> lstChild = new List<string>();
            lstChild.Add("ParameterName");
            lstChild.Add(ServiceParameter.ParameterName);
            lstParent.Add(lstChild);
            lstChild = new List<string>();
            lstChild.Add("ParameterReplaceString");
            lstChild.Add(ServiceParameter.ParameterReplaceString);
            lstParent.Add(lstChild);
            lstChild = new List<string>();
            lstChild.Add("ParameterValue");
            lstChild.Add(ServiceParameter.ParameterValue);
            lstParent.Add(lstChild);
            lstChild = new List<string>();
            lstChild.Add("IsDynamic");
            lstChild.Add(ServiceParameter.IsDynamic.ToString());
            lstParent.Add(lstChild);
            lstChild = new List<string>();
            lstChild.Add("ParameterID");
            lstChild.Add(ServiceParameter.ParameterID.ToString());
            lstParent.Add(lstChild);

            CRMFramework.DBhelper.ExecuteNonQuery(webserviceParamterSaveSQL, lstParent);
        }
    }

}
